package com.example.javabasic.suanfa.jiandan.栈与队列;

import org.assertj.core.internal.bytebuddy.implementation.bytecode.Throw;

import java.util.Stack;

/**
 * @description 栈实现队列
 * @Author Lin FuYuan
 * @date 2021/5/19
 */
public class StackOfQueue {
    private Stack<Integer> inStack;
    private Stack<Integer> outStack;
    //数据转移状态
    private boolean geting = false;

    public StackOfQueue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public void push(int n) {
        if(!geting){
            inStack.push(n);
        }
        throw new RuntimeException("正在转移数据 无法插入,请稍后");
    }

    //查看队列头部
    public int peek() {
        if(!outStack.empty()){
            return outStack.peek();
        }
        geting();
        return outStack.peek();
    }

    //转移数据过程
    public void geting(){
        //数据转移,如果outStack 不空 则跳过
        if(!outStack.empty()){
            return;
        }
        geting = true;
        while(!inStack.empty() ){
            outStack.push(inStack.pop());
        }
        geting = false;
    }

    public int pop() {
        //如果出栈为空 则 转移数据
        geting();
        //让出栈有数据后从栈中取出元素
        if(!outStack.empty()){
            Integer pop = outStack.pop();
            return pop;
        }
        throw new RuntimeException("队列为空");

    }

    public boolean empty() {
        if(outStack.empty()&& inStack.empty()){
            return true;
        }
        return false;
    }

}
